﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Sec;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.UICommon;

namespace ESOL.WinUI.Sec
{
    public partial class frmManageUser : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private SecUserEntity mUserEntity;
        #endregion

        #region Constructor
        public frmManageUser()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void Init()
        {
            
        }

        private void BindComboBox()
        {
            UICommonFunctions.BindGridComboboxByEnum(colIsDeleted, typeof(DeleteFlag), typeof(decimal), true);
            UICommonFunctions.BindGridComboboxByEnum(colIsLocked, typeof(LockedFlag), typeof(decimal), true);
        }

        private void BindDataGrid()
        {
            DataSet ds = new DataSet();
            ds = SecUserBs.GetDSAllUsers();
            if (ds != null && ds.Tables.Count > 0)
            {
                gvMain.DataSource = ds.Tables[0];
            }
            else
            {
                gvMain.DataSource = null;
            }
        }

        #endregion

        #region Event
        private void frmUserManage_Load(object sender, EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
        }

        protected override void Click_View()
        {
            if (mUserEntity == null)
            {
                MessageBox.Show("请选择一条数据！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainUser frm = new frmMaintainUser();
            frm.ThisActionType = ActionType.View;
            frm.UserEntity = mUserEntity;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mUserEntity == null)
            {
                MessageBox.Show("请选择一条数据！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确定删除？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mUserEntity.IsDelete = 1;
                bool successful = SecUserBs.UpdateUser(mUserEntity);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mUserEntity == null)
            {
                MessageBox.Show("请选择一条数据！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainUser frm = new frmMaintainUser();
            frm.ThisActionType = ActionType.Modify;
            frm.UserEntity = mUserEntity;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        //修改密码
        protected override void Click_Submit()
        {
            frmChangePassword frm = new frmChangePassword();
            frm.ShowDialog();
        }

        //重置密码
        protected override void Click_Approve()
        {
            if (mUserEntity == null)
            {
                return;
            }
            DialogResult dr = MessageBox.Show("是否确定重置密码?", "问题", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            if (SecUserBs.SavePassword(mUserEntity, Common.CommonParameter.DefaultPassword))
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        //角色设置
        protected override void Click_Print()
        {
            if (mUserEntity == null)
            {
                MessageBox.Show("请选择一条数据！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainUserRole frm = new frmMaintainUserRole();
            frm.UserEntity = mUserEntity;
            frm.ShowDialog();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            mUserEntity = new SecUserEntity(gridView1.GetDataRow(e.RowHandle));
        }

        #endregion
    }
}
